<template>
  <div class="home" v-loading="loading">
    <div class="swiper-slide slide-one">
      <div class="page"></div>
    </div>
    <div class="swiper-slide slide-two">
      <div class="page">
        <h3>开设楼盘</h3>
        <p>Property</p>
      </div>
      <ul class="case-item">
        <li v-for="(item, index) in caseList" :key="index" v-lazy:background-image="getFirstImageUrl(item.content)">
          <router-link class="text-decoration" :to="{ name: 'caseDetails', params: { id: item.id } }">
            <div class="case-item-hover">
              <div class="group-title">
                <p class="hover-title" :title="item.title">{{ item.title }}</p>
                <div class="bottom"></div>
              </div>
              <div class="more">
                <span>MORE</span>
              </div>
            </div>
          </router-link>
        </li>
      </ul>
    </div>
    <div class="swiper-slide slide-three">
      <div class="page">
        <h3>最新资讯</h3>
        <p>Latest News</p>
      </div>
      <div class="news-content">
        <div class="news-content-item" v-for="(news, i) in newsList" :key="i">
          <router-link :to="{ name: 'newsDetails', params: { id: news.id } }" class="text-decoration">
            <div class="item-img" v-lazy:background-image="getFirstImageUrl(news.content)"></div>
            <div class="item-content">
              <h3 :title="news.title">{{ news.title }}</h3>
            </div>
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      loading: true,
      caseList: [],
      newsList: [],
    };
  },
  mounted() {
    Promise.all([
      this.$http.get("case/all"),
      this.$http.get("news/all")
    ]).then(([responseCases, responseNews]) => {
      this.caseList = responseCases.data;
      this.newsList = responseNews.data;
      // 格式化日期
      // 按时间排序，最新的在前面
      this.newsList.sort((a, b) => {
        return new Date(b.createTime) - new Date(a.createTime);
      });
      this.dateFormat(this.newsList);
      this.loading = false;
    }).catch(error => {
      console.error('请求出错:', error);
    });
  },
  methods: {
    dateFormat(list) {
      list.forEach((e) => {
        let t = new Date(e.createTime);
        e.createTime =
          t.getFullYear() + "-" + (t.getMonth() + 1) + "-" + t.getDate();
      });
    },
    getFirstImageUrl(content) {
      const imgRegex = /<img[^>]+src="([^">]+)"/;
      const match = content.match(imgRegex);
      return match ? match[1] : "";
    },
  },
};
</script>

<style lang="scss" scoped>
.home {
  .swiper-slide {
    padding: 6vh 0;
    font-size: 24px;
    display: flex;
    flex-direction: column;

    .page {
      text-align: center;
      overflow: hidden;

      p {
        font-size: 36px;
        font-weight: 300;
      }

      h3 {
        font-size: 56px;
        font-weight: 600;
      }

      @media screen and (max-width: 700px) {
        p {
          font-size: 20px;
        }

        h3 {
          font-size: 25px;
        }
      }
    }
  }

  .case-item {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-gap: 2px;
    margin: 0 auto;
    padding: 0;

    li {
      width: 30vw;
      height: 28vw;
      max-width: 300px;
      max-height: 280px;
      list-style: none;
      background-repeat: no-repeat;
      background-size: cover;
      background-position: center;
      background-origin: content-box;
      position: relative;
      overflow: hidden;

      &:hover {
        .case-item-hover {
          opacity: 1;
          transition: all 0.4s ease-in-out;
        }
      }
    }
  }

  @media screen and (min-width: 1800px) {
    .case-item {
      grid-template-columns: repeat(6, 1fr);

      li {
        width: 15vw;
        height: 14vw;
      }
    }
  }

  .case-item-hover {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0;
    overflow: hidden;
    background-color: rgba(225, 56, 52, 0.7);
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;

    .hover-title {
      padding: 0 10px;
      text-align: center;
      color: #fff;
      font-size: 18px;
      text-overflow: ellipsis;
      white-space: nowrap;
      font-weight: 400;
      overflow: hidden;
    }

    .bottom {
      border-bottom: 2px solid #fff;
      width: 60px;
      margin: 0 auto;
    }

    .more {
      width: 30%;
      min-width: 60px;
      padding: 5px 5px;
      margin: 0 auto;
      margin-bottom: 10px;
      border: 2px solid #fff;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      span {
        color: #fff;
        font-size: 20px;
      }
    }
  }

  .slide-one {
    background: url("../assets/img/mbpy.webp") no-repeat center;
    background-size: 100% 100%;
    height: 45vw;
    justify-content: center;
    align-items: start;
    color: #000;
    padding: 0;
  }

  .slide-three {
    background: #f5f5f5;
  }

  .news-content {
    margin: 0 auto;

    &-item {
      background: transparent;
      width: 88vw;
      margin-bottom: 20px;

      .item-img {
        width: 88vw;
        height: 50vw;
        background-repeat: no-repeat;
        background-size: cover;
        background-position: center;
        background-origin: content-box;
      }

      .item-content {
        h3 {
          color: #000;
          margin: 5px;
          font-size: 16px;
          white-space: normal;
        }
      }
    }
  }

  @media screen and (min-width: 1000px) {
    .slide-one {
      height: 40vw;
    }

    .news-content {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      justify-items: center;
      grid-gap: 2vw;

      &-item {
        width: 28vw;
        margin: 0;

        .item-content {
          h3 {
            font-size: 16px;
          }
        }

        .item-img {
          width: 28vw;
          height: 26vw;
        }
      }
    }
  }
}

.text-decoration {
  text-decoration: none;
}
</style>
